package com.shop.model.util;

import com.shop.model.entity.AttributeTb;

import java.util.ArrayList;
import java.util.HashMap;

public class AttributeUtil {
    /**
     *将属性数组转换成map，让属性名和属性值的数组对应起来
     * 作者：谢安建
     * @param attributeTbs
     * @return
     */
    public static HashMap<String, String[]> attributeReorganization(ArrayList<AttributeTb> attributeTbs){
        HashMap<String, String[]> attributeMap = new HashMap<>();
        //遍历属性数组
        for (AttributeTb attributeTb : attributeTbs) {
            //获取出属性数组的name和value
            String name = attributeTb.getName();
            String value = attributeTb.getValue();
            String[] values = null;
            boolean isHave = false;
            //把属性名相同的属性组装进一个数组
            for (String s : attributeMap.keySet()) {
                if (name.equals(s)){
                    isHave=true;
                    String[] strings = attributeMap.get(s);
                    values = new String[strings.length+1];
                    values[0] = value;
                    for (int i = 0; i < strings.length; i++) {
                        values[i+1] = strings[i];
                    }
                }

            }
            if (!isHave){
                values = new String[] {value};
            }

            attributeMap.put(name,values);
        }
        return attributeMap;
    }
}
